package com.bjfu.mapper;

import com.bjfu.model.Question;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface QuestionMapper {

    // 插入一个新的问题
    @Insert("INSERT INTO question (title,content, user_id, username,question_time,status, reply_content, reply_time) VALUES (#{title},#{content}, #{userId},#{username},#{questionTime},#{status}, #{replyContent}, #{replyTime})")
    @Options(useGeneratedKeys = true, keyProperty = "questionId", keyColumn = "question_id")
    int insertQuestion(Question question);

    // 更新问题信息
    @Update("UPDATE question SET content = #{content}, status = #{status}, reply_content = #{replyContent}, reply_time = #{replyTime} WHERE question_id = #{questionId}")
    void updateQuestion(Question question);

    // 删除问题
    @Delete("DELETE FROM question WHERE question_id = #{questionId}")
    void deleteQuestion(Long questionId);

    // 根据问题 ID 获取问题
    @Select("SELECT * FROM question WHERE question_id = #{questionId}")
    Question getQuestionById(Long questionId);

    // 获取所有问题
    @Select("SELECT * FROM question")
    List<Question> getAllQuestions();

    // 根据用户 ID 获取该用户提问的所有问题
    @Select("SELECT * FROM question WHERE user_id = #{userId}")
    List<Question> getQuestionsByUserId(Long userId);

    // 根据用户 ID 和问题状态获取问题
    @Select("SELECT * FROM question WHERE user_id = #{userId} AND status = #{status}")
    List<Question> getQuestionsByUserIdAndStatus(Long userId, Integer status);

    // 根据问题状态查询所有问题
    @Select("SELECT * FROM question WHERE status = #{status}")
    List<Question> getQuestionsByStatus(Integer status);

    @Update("UPDATE question SET reply_content = #{reply_content}, reply_time = #{reply_time}, status = 1 WHERE question_id = #{question_id}")
    int answerQuestion(Long question_id, String reply_content, LocalDateTime reply_time);
    @Update("UPDATE question SET status = #{status} WHERE question_id = #{question_id}")
    int updateQuestionStatus(Long question_id, int status);
}
